package com.shanghaiyimu.yimiyangche.activity;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.shanghaiyimu.yimiyangche.MyApplication;
import com.shanghaiyimu.yimiyangche.R;
import com.shanghaiyimu.yimiyangche.adapter.CheXiAdapter;
import com.shanghaiyimu.yimiyangche.adapter.CheXingAdapter;
import com.shanghaiyimu.yimiyangche.adapter.CityAdapter;
import com.shanghaiyimu.yimiyangche.adapter.PinPaiAdapter;
import com.shanghaiyimu.yimiyangche.adapter.ProvinceAdapter;
import com.shanghaiyimu.yimiyangche.bean.Pinpaipinpia;
import com.shanghaiyimu.yimiyangche.entity.CheXiEntity;
import com.shanghaiyimu.yimiyangche.entity.CheXingEntity;
import com.shanghaiyimu.yimiyangche.entity.City;
import com.shanghaiyimu.yimiyangche.entity.JingQueEntity;
import com.shanghaiyimu.yimiyangche.entity.Provence;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 key 	string 	是 	应用APPKEY(应用详细页查询)
 dtype 	string 	否 	返回数据的格式,xml或json，默认json
 carstatus 	int 	是 	车况，较差3，一般2，优秀1
 purpose 	int 	是 	车辆用途: 1自用 2公务商用 3营运
 city 	int 	是 	城市标识（从“二手车价值评估/城市列表”接口获取)
 province 	int 	是 	省份标识（从“二手车价值评估/省份列表”接口获取)
 car 	int 	是 	车型标识（从“二手车价值评估/车型列表”接口获取)
 useddate 	int 	是 	待估车辆的启用年份（格式：yyyy）
 useddateMonth 	string 	是 	待估车辆的启用月份（格式：mm）
 mileage 	double 	是 	待估车辆的公里数，单位万公里
 price 	double 	是 	待估车辆在购买价(单位万元)
 */
public class CheLiangPingguActivity extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener ,AdapterView.OnItemClickListener{
    private TextView sheng, shi,pinpai, chexing, chexi, jieguo;
    private RadioGroup radioGroup,carstatus,purpose;
    private Button guzhi;
    private EditText useddateEditText,useddateMonthEditText,mileageEditText,priceEditText;
    private String useddate,useddateMonth,mileage,price;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_che_liang_pinggu);
        initView();
    }

    private void initView() {
        carstatus = (RadioGroup) findViewById(R.id.chekuang);
        purpose = (RadioGroup) findViewById(R.id.yongtu);
        useddateEditText = (EditText) findViewById(R.id.qiyongnianfen);
        useddateMonthEditText = (EditText) findViewById(R.id.qiyongyuefen);
        mileageEditText = (EditText) findViewById(R.id.gonglishu);
        priceEditText = (EditText) findViewById(R.id.goumaijia);
        sheng = (TextView) findViewById(R.id.diquxuanze);
        shi = (TextView) findViewById(R.id.chengshixuanze);
        pinpai= (TextView) findViewById(R.id.pinpaixuanze);
        chexi = (TextView) findViewById(R.id.chengxixuanze);
        chexing = (TextView) findViewById(R.id.chengxingxuanze);
        jieguo = (TextView) findViewById(R.id.jieguo);
        radioGroup = (RadioGroup) findViewById(R.id.pingguradiogroup);
        guzhi = (Button) findViewById(R.id.jingqueguzhi);
        sheng.setOnClickListener(this);
        shi.setOnClickListener(this);
        pinpai.setOnClickListener(this);
        chexi.setOnClickListener(this);
        chexing.setOnClickListener(this);
        guzhi.setOnClickListener(this);
        radioGroup.setOnCheckedChangeListener(this);
        carstatus.setOnCheckedChangeListener(this);
        purpose.setOnCheckedChangeListener(this);
    }


    //配置您申请的KEY
    public static final String APPKEY = "3b0443cc9fa84c66b2240df10487b644";
//    public static final String APPKEY = "5ec2a63bcff173b8706e0007d76130c2";
    String result = null;
    List<Provence.ResultBean> resultPro = new ArrayList<>();
    List<City.ResultBean> resultCity = new ArrayList<>();
    List<Pinpaipinpia> resultPinpai = new ArrayList<>();
    List<CheXiEntity.ResultBean.PinpaiListBean> resultChexi = new ArrayList<>();
    List<CheXingEntity.ResultBean.DataBean> resultChexing = new ArrayList<>();

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.diquxuanze:

                String url = "http://v.juhe.cn/usedcar/province";//请求接口地址
                volleypost(url);
                initPopView();

                break;
            case R.id.chengshixuanze:
                if (proid != null) {
                    String urlCity = "http://v.juhe.cn/usedcar/city";//请求接口地址
                    volleypostCity(urlCity);
                    initCityPopView();
                }else {
                    Toast.makeText(CheLiangPingguActivity.this,"请选择省份",Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.pinpaixuanze:

                if (vehicle != null) {

                    String urlPinpai = "http://v.juhe.cn/usedcar/brand";//请求接口地址
                    volleypostPinPai(urlPinpai);
                    initPinpaiPopWindow();

                }else {
                    Toast.makeText(CheLiangPingguActivity.this,"请选择使用类型",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.chengxixuanze:
                String urlChexi = "http://v.juhe.cn/usedcar/series";//请求接口地址
                volleypostChexi(urlChexi);
                initCheXiPopWindow();
                break;
            case R.id.chengxingxuanze:
                String urlChexing = "http://v.juhe.cn/usedcar/car";//请求接口地址
                volleypostChexing(urlChexing);
                initCheXingPopWindow();
                break;
            //button 精确估值
            case R.id.jingqueguzhi:
                String urlJingque = "http://v.juhe.cn/usedcar/assess";//请求接口地址
                useddate = useddateEditText.getText().toString();
                useddateMonth = useddateMonthEditText.getText().toString();
                mileage = mileageEditText.getText().toString();
                price = priceEditText.getText().toString();
                volleypostJingQue(urlJingque);

                break;
        }
    }
    //精确查询
    private void volleypostJingQue(String urlJingque) {
        StringRequest request = new StringRequest(Request.Method.POST, urlJingque, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.e("=====", "onResponse: "+s );
                JingQueEntity jingQue= new Gson().fromJson(s, JingQueEntity.class);
                List<Double> est_price_result = jingQue.getResult().getEst_price_result();
                jieguo.setText("收购价格："+est_price_result.get(0)+"万元\n"+"个人交易价格："+est_price_result.get(1)+"万元\n"+"卖出价格："+est_price_result.get(2)+"万元");
//                resultChexing.clear();
//                resultChexing.addAll(cheXing.getResult().getData());
//                showCheXingPopWindow();
//                mCheXingAdapter.notifyDataSetChanged();
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

                result = volleyError.toString();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<>();
//                map
                map.clear();
                map.put("dtype=", "");
                map.put("key", APPKEY);//应用APPKEY(应用详细页查询)
                map.put("carstatus", carstatusCar);
                map.put("purpose", purposeCar);
                map.put("city", cityid);
                map.put("province", proid);
                map.put("car", chexingId);
                map.put("useddate", useddate);
                map.put("useddateMonth", useddateMonth);
                map.put("mileage", mileage);
                map.put("price", price);
                return map;
            }
        };
        request.setTag("volleypost");
        MyApplication.getHttpQueue().add(request);
    }
    //车型请求
    private void volleypostChexing(String urlChexing) {
        StringRequest request = new StringRequest(Request.Method.POST, urlChexing, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.e("=====", "onResponse: "+s );
                CheXingEntity cheXing= new Gson().fromJson(s, CheXingEntity.class);
                Log.e("======", "onResponse: "+ cheXing.getResult().getData().get(0).getChexing_list().get(0).getCxname());
                resultChexing.clear();
                resultChexing.addAll(cheXing.getResult().getData());
                showCheXingPopWindow();
                mCheXingAdapter.notifyDataSetChanged();
            }


        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

                result = volleyError.toString();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<>();
//                map
                map.clear();
                map.put("dtype=", "");
                map.put("key", APPKEY);//应用APPKEY(应用详细页查询)
                map.put("series", chexiId);
                return map;
            }
        };
        request.setTag("volleypost");
        MyApplication.getHttpQueue().add(request);
    }

    //车系请求
    private void volleypostChexi(String urlChexi) {
        StringRequest request = new StringRequest(Request.Method.POST, urlChexi, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                CheXiEntity cheXi= new Gson().fromJson(s, CheXiEntity.class);
                resultChexi.clear();
                resultChexi.addAll(cheXi.getResult().getPinpai_list());
                showCheXiPopWindow();
                mCheXiAdapter.notifyDataSetChanged();
            }


        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.i("aa", "post请求失败" + volleyError.toString());
                result = volleyError.toString();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<>();
//                map
                map.clear();
                map.put("dtype=", "");
                map.put("key", APPKEY);//应用APPKEY(应用详细页查询)
                map.put("brand", pinpaiId);
                return map;
            }
        };
        request.setTag("volleypost");
        MyApplication.getHttpQueue().add(request);
    }

    // 	车辆类型,车况，用途
    private  String vehicle,carstatusCar,purposeCar;
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId){
                //乘用车
                case R.id.passenger:
                    vehicle = "passenger";
                    break;
                //商用车
                case R.id.commercial:
                    vehicle = "commercial";
                    break;
                case R.id.youxiu:
                    carstatusCar = "1";
                    break;
                case R.id.yiban:
                    carstatusCar = "2";
                    break;
                case R.id.jiaoca:
                    carstatusCar = "3";
                    break;
                case R.id.ziyong:
                    purposeCar = "1";
                    break;
                case R.id.gongwushangyong:
                    purposeCar = "2";
                    break;
                case R.id.yingyun:
                    purposeCar = "3";
                    break;
        }

    }

    //volley发送post请 省份
    private void volleypost(String url) {
        StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.e("-=-=-=-=-=", "onResponse: "+s);
                Provence provence = new Gson().fromJson(s, Provence.class);
                resultPro.clear();
                resultPro.addAll(provence.getResult());
                showPopWindow();
                mProvinceAdapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.i("aa", "post请求失败" + volleyError.toString());
                result = volleyError.toString();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<>();
//                map
                map.clear();
                map.put("dtype=", "");
                map.put("key", APPKEY);//应用APPKEY(应用详细页查询)
                return map;
            }
        };
        request.setTag("volleypost");
        MyApplication.getHttpQueue().add(request);
    }

    //volley发送post请 城市
    private void volleypostCity(String url) {
        StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
               City city = new Gson().fromJson(s, City.class);
                resultCity.clear();
                resultCity.addAll(city.getResult());
                showCityPopWindow();
                mCityAdapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.i("aa", "post请求失败" + volleyError.toString());
                result = volleyError.toString();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<>();
//                map
                map.clear();
                map.put("dtype=", "");
                map.put("key", APPKEY);//应用APPKEY(应用详细页查询)
                map.put(" province", proid);
                return map;
            }
        };
        request.setTag("volleypost");
        MyApplication.getHttpQueue().add(request);
    }
    // 发送post请 品牌
    private void volleypostPinPai(String url){
        StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
//                PinPaiList.ResultBean.ABean.PinPai pinpaia = new Gson().fromJson(response, PinPaiList.ResultBean.ABean.PinPai.class);
//                PinPaiList.ResultBean.BBean.PinPai pinpaib = new Gson().fromJson(response, PinPaiList.ResultBean.BBean.PinPai.class);
//                PinPaiList.ResultBean.CBean.PinPai pinpaic = new Gson().fromJson(response, PinPaiList.ResultBean.CBean.PinPai.class);
//                PinPaiList.ResultBean.DBean.PinPai pinpaid = new Gson().fromJson(response, PinPaiList.ResultBean.DBean.PinPai.class);
//                PinPaiList.ResultBean.EBean.PinPai pinpaie = new Gson().fromJson(response, PinPaiList.ResultBean.EBean.PinPai.class);
//                PinPaiList.ResultBean.FBean.PinPai pinpaif = new Gson().fromJson(response, PinPaiList.ResultBean.FBean.PinPai.class);
//                PinPaiList.ResultBean.GBean.PinPai pinpaig = new Gson().fromJson(response, PinPaiList.ResultBean.GBean.PinPai.class);
//                PinPaiList.ResultBean.HBean.PinPai pinpaih = new Gson().fromJson(response, PinPaiList.ResultBean.HBean.PinPai.class);
//                PinPaiList.ResultBean.IBean.PinPai pinpaii = new Gson().fromJson(response, PinPaiList.ResultBean.IBean.PinPai.class);
//                PinPaiList.ResultBean.JBean.PinPai pinpaij = new Gson().fromJson(response, PinPaiList.ResultBean.JBean.PinPai.class);
//                PinPaiList.ResultBean.KBean.PinPai pinpaik = new Gson().fromJson(response, PinPaiList.ResultBean.KBean.PinPai.class);
//                PinPaiList.ResultBean.LBean.PinPai pinpail = new Gson().fromJson(response, PinPaiList.ResultBean.LBean.PinPai.class);
//                PinPaiList.ResultBean.MBean.PinPai pinpaim = new Gson().fromJson(response, PinPaiList.ResultBean.MBean.PinPai.class);
//                PinPaiList.ResultBean.NBean.PinPai pinpain = new Gson().fromJson(response, PinPaiList.ResultBean.NBean.PinPai.class);
//                PinPaiList.ResultBean.OBean.PinPai pinpaio = new Gson().fromJson(response, PinPaiList.ResultBean.OBean.PinPai.class);
//                PinPaiList.ResultBean.PBean.PinPai pinpaip = new Gson().fromJson(response, PinPaiList.ResultBean.PBean.PinPai.class);
//                PinPaiList.ResultBean.QBean.PinPai pinpaiq = new Gson().fromJson(response, PinPaiList.ResultBean.QBean.PinPai.class);
//                PinPaiList.ResultBean.RBean.PinPai pinpair = new Gson().fromJson(response, PinPaiList.ResultBean.RBean.PinPai.class);
//                PinPaiList.ResultBean.SBean.PinPai pinpais = new Gson().fromJson(response, PinPaiList.ResultBean.SBean.PinPai.class);
//                PinPaiList.ResultBean.TBean.PinPai pinpait = new Gson().fromJson(response, PinPaiList.ResultBean.TBean.PinPai.class);
//                PinPaiList.ResultBean.UBean.PinPai pinpaiu = new Gson().fromJson(response, PinPaiList.ResultBean.UBean.PinPai.class);
//                PinPaiList.ResultBean.VBean.PinPai pinpaiv = new Gson().fromJson(response, PinPaiList.ResultBean.VBean.PinPai.class);
//                PinPaiList.ResultBean.WBean.PinPai pinpaiw = new Gson().fromJson(response, PinPaiList.ResultBean.WBean.PinPai.class);
//                PinPaiList.ResultBean.XBean.PinPai pinpaix = new Gson().fromJson(response, PinPaiList.ResultBean.XBean.PinPai.class);
//                PinPaiList.ResultBean.YBean.PinPai pinpaiy = new Gson().fromJson(response, PinPaiList.ResultBean.YBean.PinPai.class);
//                PinPaiList.ResultBean.ZBean.PinPai pinpaiz = new Gson().fromJson(response, PinPaiList.ResultBean.ZBean.PinPai.class);
                resultPinpai.clear();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONObject result = jsonObject.getJSONObject("result");
                    JSONArray a = result.getJSONArray("A");
                    if (a != null) {
                        for (int i = 0; i < a.length(); i++) {
                            JSONObject o = a.getJSONObject(i);
                            String id = o.getString("id");
                            String big_ppname = o.getString("big_ppname");
                            String pin = o.getString("pin");
                            Pinpaipinpia pinpaipinpia = new Pinpaipinpia();
                            pinpaipinpia.setId(id);
                            pinpaipinpia.setBig_ppname(big_ppname);
                            pinpaipinpia.setPin(pin);
                            Log.e("-=-=-=-=-=-", "onResponse: "+pinpaipinpia.getBig_ppname() );
                            resultPinpai.add(pinpaipinpia);
                        }
                    }

                    JSONArray b = result.getJSONArray("B");
                    if (b != null) {
                        for (int i = 0; i < b.length(); i++) {
                            JSONObject o = b.getJSONObject(i);
                            String id = o.getString("id");
                            String big_ppname = o.getString("big_ppname");
                            String pin = o.getString("pin");
                            Pinpaipinpia pinpaipinpia = new Pinpaipinpia();
                            pinpaipinpia.setId(id);
                            pinpaipinpia.setBig_ppname(big_ppname);
                            pinpaipinpia.setPin(pin);
                            resultPinpai.add(pinpaipinpia);
                            Log.e("-=-=-=-=-=-", "onResponse: "+pinpaipinpia.getBig_ppname() );
                        }
                    }
                    JSONArray c = result.getJSONArray("C");
                    if (c != null) {
                        for (int i = 0; i < c.length(); i++) {
                            JSONObject o = c.getJSONObject(i);
                            String id = o.getString("id");
                            String big_ppname = o.getString("big_ppname");
                            String pin = o.getString("pin");
                            Pinpaipinpia pinpaipinpia = new Pinpaipinpia();
                            pinpaipinpia.setId(id);
                            pinpaipinpia.setBig_ppname(big_ppname);
                            pinpaipinpia.setPin(pin);
                            resultPinpai.add(pinpaipinpia);
                        }
                    }

                    JSONArray d = result.getJSONArray("D");

                    if (d != null) {
                        for (int i = 0; i < d.length(); i++) {
                            JSONObject o = d.getJSONObject(i);
                            String id = o.getString("id");
                            String big_ppname = o.getString("big_ppname");
                            String pin = o.getString("pin");
                            Pinpaipinpia pinpaipinpia = new Pinpaipinpia();
                            pinpaipinpia.setId(id);
                            pinpaipinpia.setBig_ppname(big_ppname);
                            pinpaipinpia.setPin(pin);
                            resultPinpai.add(pinpaipinpia);
                        }
                    }

                    JSONArray e = result.getJSONArray("E");
                    if (e != null) {
                        for (int i = 0; i < e.length(); i++) {
                            JSONObject o = e.getJSONObject(i);
                            String id = o.getString("id");
                            String big_ppname = o.getString("big_ppname");
                            String pin = o.getString("pin");
                            Pinpaipinpia pinpaipinpia = new Pinpaipinpia();
                            pinpaipinpia.setId(id);
                            pinpaipinpia.setBig_ppname(big_ppname);
                            pinpaipinpia.setPin(pin);
                            resultPinpai.add(pinpaipinpia);
                        }
                    }

                } catch (JSONException e) {

                }
                showPianPaiPopWindow();
                mPinpaiAdapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("-=-=-=-", "onErrorResponse: "+"error" );
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<>();
//                map
                map.clear();
                map.put("dtype=", "");
                map.put("key", APPKEY);//应用APPKEY(应用详细页查询)
                map.put("vehicle",vehicle);
                return map;
            }
        };
        request.setTag("volleypost");
        MyApplication.getHttpQueue().add(request);
    }
    @Override
    protected void onStop() {
        super.onStop();
        MyApplication.getHttpQueue().cancelAll("volleypost");
    }

    private PopupWindow mPopWindow,mCityPopWindow,mPinpaiPopWindow,mChexiPopWindow,mCheximgPopWindow;
    private View parent;
    private TextView mTitle;
    private ListView mProvinceListView,mCityListView,mPinpaiListView,mChexiListView,mChexingListView;
    private ProvinceAdapter mProvinceAdapter;
    private CityAdapter mCityAdapter;
    private PinPaiAdapter mPinpaiAdapter;
    private CheXiAdapter mCheXiAdapter;
    private CheXingAdapter mCheXingAdapter;
    private String proid,cityid,pinpaiId,chexiId,chexingId;
    private void initPopView() {
        parent = this.getWindow().getDecorView();
        View popView = View.inflate(this, R.layout.view_select_province_list, null);
//        mTitle = (TextView) popView.findViewById(R.id.list_title);
//        mTitle.setText(title);
        mProvinceListView = (ListView) popView.findViewById(R.id.province);
        mProvinceListView.setOnItemClickListener(this);
        mProvinceAdapter = new ProvinceAdapter(this, resultPro);
        mProvinceListView.setAdapter(mProvinceAdapter);

        int width = getResources().getDisplayMetrics().widthPixels * 3 / 4;
        int height = getResources().getDisplayMetrics().heightPixels * 3 / 5;
        mPopWindow = new PopupWindow(popView, width, height);
        ColorDrawable dw = new ColorDrawable(0x30000000);
        mPopWindow.setBackgroundDrawable(dw);
        mPopWindow.setFocusable(true);
        mPopWindow.setTouchable(true);
        mPopWindow.setOutsideTouchable(true);//允许在外侧点击取消
        mPopWindow.setOnDismissListener(listener);
    }

    // 城市品牌
    private void initPinpaiPopWindow() {
        parent = this.getWindow().getDecorView();
        View popView = View.inflate(this, R.layout.view_listview_pop_pinpai, null);
        mPinpaiListView = (ListView) popView.findViewById(R.id.pinpai_listview_pop);
        mPinpaiListView.setOnItemClickListener(this);

        mPinpaiAdapter = new PinPaiAdapter(this,resultPinpai);
        mPinpaiListView.setAdapter(mPinpaiAdapter);

        int width = getResources().getDisplayMetrics().widthPixels * 3 / 4;
        int height = getResources().getDisplayMetrics().heightPixels * 3 / 5;
        mPinpaiPopWindow = new PopupWindow(popView, width, height);
        ColorDrawable dw = new ColorDrawable(0x30000000);
        mPinpaiPopWindow.setBackgroundDrawable(dw);
        mPinpaiPopWindow.setFocusable(true);
        mPinpaiPopWindow.setTouchable(true);
        mPinpaiPopWindow.setOutsideTouchable(true);//允许在外侧点击取消
//        mCityPopWindow.setOnDismissListener(listener);
    }

    // 城市选择
    private void initCityPopView() {
        parent = this.getWindow().getDecorView();
        View popView = View.inflate(this, R.layout.view_select_city_list, null);
        mCityListView = (ListView) popView.findViewById(R.id.city);
        mCityListView.setOnItemClickListener(this);
        mCityAdapter = new CityAdapter(this, resultCity);
        mCityListView.setAdapter(mCityAdapter);

        int width = getResources().getDisplayMetrics().widthPixels * 3 / 4;
        int height = getResources().getDisplayMetrics().heightPixels * 3 / 5;
        mCityPopWindow = new PopupWindow(popView, width, height);
        ColorDrawable dw = new ColorDrawable(0x30000000);
        mCityPopWindow.setBackgroundDrawable(dw);
        mCityPopWindow.setFocusable(true);
        mCityPopWindow.setTouchable(true);
        mCityPopWindow.setOutsideTouchable(true);//允许在外侧点击取消
//        mCityPopWindow.setOnDismissListener(listener);

    }
    // 车系选择
    private void initCheXiPopWindow() {
        parent = this.getWindow().getDecorView();
        View popView = View.inflate(this, R.layout.view_select_chexi_listview, null);
        mChexiListView = (ListView) popView.findViewById(R.id.chexi_listview_pop);
        mChexiListView.setOnItemClickListener(this);
        mCheXiAdapter = new CheXiAdapter(this, resultChexi);
        mChexiListView.setAdapter(mCheXiAdapter);
        mCheXiAdapter.set(chexis);


        int width = getResources().getDisplayMetrics().widthPixels * 3 / 4;
        int height = getResources().getDisplayMetrics().heightPixels * 3 / 5;
        mChexiPopWindow = new PopupWindow(popView, width, height);
        ColorDrawable dw = new ColorDrawable(0x30000000);
        mChexiPopWindow.setBackgroundDrawable(dw);
        mChexiPopWindow.setFocusable(true);
        mChexiPopWindow.setTouchable(true);
        mChexiPopWindow.setOutsideTouchable(true);//允许在外侧点击取消
//        mCityPopWindow.setOnDismissListener(listener);

    }
    //车型选择
    private void initCheXingPopWindow() {
        parent = this.getWindow().getDecorView();
        View popView = View.inflate(this, R.layout.view_select_chexing_popview, null);
        mChexingListView = (ListView) popView.findViewById(R.id.chexing_listview_pop);
        mChexingListView.setOnItemClickListener(this);
        mCheXingAdapter = new CheXingAdapter(this, resultChexing);
        mChexingListView.setAdapter(mCheXingAdapter);
        mCheXingAdapter.set(s);
        int width = getResources().getDisplayMetrics().widthPixels * 3 / 4;
        int height = getResources().getDisplayMetrics().heightPixels * 3 / 5;
        mCheximgPopWindow = new PopupWindow(popView, width, height);
        ColorDrawable dw = new ColorDrawable(0x30000000);
        mCheximgPopWindow.setBackgroundDrawable(dw);
        mCheximgPopWindow.setFocusable(true);
        mCheximgPopWindow.setTouchable(true);
        mCheximgPopWindow.setOutsideTouchable(true);//允许在外侧点击取消
//        mCityPopWindow.setOnDismissListener(listener);

    }

    private void showPopWindow() {
        mPopWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }
    private void showCityPopWindow() {
        mCityPopWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }
    private void showPianPaiPopWindow() {
        mPinpaiPopWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }
    private void showCheXiPopWindow() {
        mChexiPopWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }
    private void showCheXingPopWindow() {
        mCheximgPopWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }


    /**
     * popWindow消失监听事件
     */
    PopupWindow.OnDismissListener listener = new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {

        }
    };

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (mProvinceListView == parent){
            sheng.setText(((TextView) view.findViewById(R.id.provence_name)).getText().toString());
            proid = (String) view.findViewById(R.id.provence_name).getTag();
//            proId = Integer.parseInt(proid);
            Log.e("=====", "onItemClick: 省份"+proid );
            mPopWindow.dismiss();
        }
        if (mCityListView == parent){
            shi.setText(((TextView) view.findViewById(R.id.city_list_item)).getText().toString());
            cityid = (String) view.findViewById(R.id.city_list_item).getTag();
            Log.e("=====", "onItemClick:市区 "+proid );
            mCityPopWindow.dismiss();
        }
        if (mPinpaiListView == parent){

            pinpai.setText(((TextView) view.findViewById(R.id.list_lv_item_pinpia)).getText().toString());
            pinpaiId = (String) view.findViewById(R.id.list_lv_item_pinpia).getTag();
            Log.e("=====", "onItemClick:品牌 "+pinpaiId );
            mPinpaiPopWindow.dismiss();
        }

    }


    CheXingAdapter.SetCheXing s = new CheXingAdapter.SetCheXing() {
        @Override
        public void onChngeCheXing(String s, String chexingids) {
            Log.e("===", "onChngeCheXing: "+s+"id"+chexingids);
            chexing.setText(s);
            chexingId = chexingids;
            mCheximgPopWindow.dismiss();
        }
    };
    CheXiAdapter.SetCheXi chexis = new CheXiAdapter.SetCheXi() {
        @Override
        public void setCheXiContent(String content, String chexiID) {
            chexi.setText(content);
            chexiId = chexiID;
            mChexiPopWindow.dismiss();
        }
    };
}
